package com.zhouyu.mybatis.spring;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @Import导入的类继承了ImportBeanDefinitionRegistrar接口时，在Spring启动时，会调用registerBeanDefinitions方法
 */
public class MyBatisImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
		// 获取MyBatisMapperScan注解上的value
		String path = (String) importingClassMetadata.getAnnotationAttributes(MyBatisMapperScan.class.getName()).get("value");
		MyBatisClassPathBeanDefinitionScanner scanner = new MyBatisClassPathBeanDefinitionScanner(registry);
		// 添加排查规则，不排除
		scanner.addIncludeFilter((metadataReader, metadataReaderFactory) -> true);
		scanner.scan(path);
	}
}
